package satir.cell.factory;

import org.json.JSONException;
import org.json.JSONObject;

import satir.card.Deck;
import satir.cell.Cell;
import satir.framework.refectoring.Factory;

/**
 * A factory for creating Cell objects.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class CellFactory implements Factory {

    /** The width. */
    protected int width;

    /** The height. */
    protected int height;

    /** The border horizontal. */
    protected char borderHorizontal;

    /** The border vertical. */
    protected char borderVertical;

    /** The deck. */
    protected Deck deck;

    /**
     * Creates a new Cell object.
     * @return the cell
     */
    public Cell createCell() {
	return new Cell(width, height, ' ', ' ', "");
    }

    /**
     * Sets the cell detail from cell JSON.
     * @param cellJson
     *            the new cell detail
     */
    public void setCellDetail(final JSONObject cellJson) {
	try {
	    this.width = cellJson.getInt("width");
	    this.height = cellJson.getInt("width");
	    this.borderHorizontal = cellJson.getString("borderHorizontal")
		    .charAt(0);
	    this.borderVertical = cellJson.getString("borderVertical")
		    .charAt(0);
	} catch (JSONException e) {
	    e.printStackTrace();
	}
    }

    /**
     * Creates the.
     * @param json
     *            the json
     * @return the object
     * @throws FactoryException
     *             the factory exception
     * @since 2.1
     * @author Torrey Tsui
     * @see Factory, LazyInitiator
     */
    @Override
    public Object create(final JSONObject json) throws FactoryException {
	setCellDetail(json);
	return createCell();
    }
}
